Ext.define('SKJF.view.PENJUALAN.v_salesrtndet_form', {
	extend	: 'Ext.form.Panel',
	
	alias	: 'widget.v_salesrtndet_form',
	
	//region:'east',
	//id: 'east-region-container',
	
	title		: 'Create/Update salesrtndet',
    bodyPadding	: 5,
    autoScroll	: true,
    
    initComponent: function(){
    	/*
		 * Deklarasi variable setiap field
		 */
		
		var do_store = Ext.create('SKJF.store.s_do',{autoLoad:true});
		var dodetail_store = Ext.create('SKJF.store.s_dodetail');
		
		var NOMOR_SALES_RETURN_field = Ext.create('Ext.form.field.Text', {
			itemId: 'NOMOR_SALES_RETURN_field',
			name: 'NOMOR_SALES_RETURN', /* column name of table */
			fieldLabel: 'NOMOR_SALES_RETURN',
			allowBlank: false /* jika primary_key */,
			maxLength: 15 /* length of column name */
		});
		var TGL_DETAIL_SALES_RETURN_field = Ext.create('Ext.form.field.Date', {
			itemId: 'TGL_DETAIL_SALES_RETURN_field',
			name: 'TGL_DETAIL_SALES_RETURN', /* column name of table */
			format: 'Y-m-d',
			fieldLabel: 'TGL_DETAIL_SALES_RETURN',
			value: new Date()
		});
		var NOMOR_DO_field = Ext.create('Ext.form.field.ComboBox', {
			itemId: 'NOMOR_DO_field',
			name: 'NOMOR_DO', /* column name of table */
			fieldLabel: 'NOMOR_DO',
			allowBlank: false,
			pageSize: 10,
			typeAhead    : true,
			triggerAction: 'all',
			selectOnFocus: true,
            loadingText  : 'Searching...',
			store: do_store,
			queryMode: 'local',
			tpl: Ext.create('Ext.XTemplate',
				'<tpl for=".">',
					'<div class="x-boundlist-item">{NOMOR_DO} - {NOMOR_INVOICE}</div>',
				'</tpl>'
			),
			displayTpl: Ext.create('Ext.XTemplate',
				'<tpl for=".">',
					'{NOMOR_DO} - {NOMOR_INVOICE}',
				'</tpl>'
			),
			valueField: 'NOMOR_DO',
			displayField: 'NOMOR_DO',
			enableKeyEvents: true,
			listeners: {
				select: function(combo, records, e){
					dodetail_store.load({
						params: {
							NOMOR_DO: records[0].data.NOMOR_DO
						}
					});
				}
			}
		});
		var TANGGAL_DO_field = Ext.create('Ext.form.field.Date', {
			name: 'TANGGAL_DO', /* column name of table */
			format: 'Y-m-d',
			fieldLabel: 'TANGGAL_DO'
		});
		var NAMA_BARANG_field = Ext.create('Ext.form.field.Text', {
			name: 'NAMA_BARANG', /* column name of table */
			fieldLabel: 'NAMA_BARANG',
			readOnly: true
		});
		var BATCH_NO_field = Ext.create('Ext.form.field.Text', {
			name: 'BATCH_NO', /* column name of table */
			fieldLabel: 'BATCH_NO',
			readOnly: true
		});
		var SATUAN_field = Ext.create('Ext.form.field.Text', {
			name: 'SATUAN', /* column name of table */
			fieldLabel: 'SATUAN',
			readOnly: true
		});
		var HARGA_SATUAN_field = Ext.create('Ext.form.field.Number', {
			name: 'HARGA_SATUAN', /* column name of table */
			fieldLabel: 'HARGA_SATUAN',
			readOnly: true
		});
		var KODE_BARANG_field = Ext.create('Ext.form.field.ComboBox', {
			name: 'KODE_BARANG', /* column name of table */
			fieldLabel: 'KODE_BARANG',
			pageSize: 10,
			typeAhead    : true,
			triggerAction: 'all',
			selectOnFocus: true,
            loadingText  : 'Searching...',
			store: dodetail_store,
			queryMode: 'local',
			tpl: Ext.create('Ext.XTemplate',
				'<tpl for=".">',
					'<div class="x-boundlist-item">{NAMA_BARANG} - {BATCH_NO}</div>',
				'</tpl>'
			),
			displayTpl: Ext.create('Ext.XTemplate',
				'<tpl for=".">',
					'{NAMA_BARANG} - {BATCH_NO}',
				'</tpl>'
			),
			valueField: 'KODE_BARANG',
			displayField: 'KODE_BARANG',
			enableKeyEvents: true,
			listeners: {
				select: function(combo, records, e){
					NAMA_BARANG_field.setValue(records[0].data.NAMA_BARANG);
					BATCH_NO_field.setValue(records[0].data.BATCH_NO);
					SATUAN_field.setValue(records[0].data.SATUAN);
					HARGA_SATUAN_field.setValue(records[0].data.HARGA_SATUAN);
					
					dodetail_store.load({
						params: {
							NOMOR_SALES_RETURN: sel.NOMOR_SALES_RETURN
						}
					});
				}
			}
		});
		var HARGA_TOTAL_field = Ext.create('Ext.form.field.Number', {
			name: 'HARGA_TOTAL', /* column name of table */
			fieldLabel: 'HARGA_TOTAL',
			readOnly: true
		});
		var QUANTITY_field = Ext.create('Ext.form.field.Number', {
			name: 'QUANTITY', /* column name of table */
			fieldLabel: 'QUANTITY',
			enableKeyEvents: true,
			listeners: {
				'keyup': function(editor, e){
					var harga = HARGA_SATUAN_field.getValue();
					var total = editor.value * harga;
					HARGA_TOTAL_field.setValue(total);
				}
			}
		});
		var ID_field = Ext.create('Ext.form.field.Hidden', {
			name: 'ID', /* column name of table */
			fieldLabel: 'ID',
			readOnly: true
		});	
        Ext.apply(this, {
            fieldDefaults: {
                labelAlign: 'right',
                labelWidth: 120,
                msgTarget: 'qtip',
				anchor: '100%'
            },
			defaultType: 'textfield',
            items: [NOMOR_SALES_RETURN_field,TGL_DETAIL_SALES_RETURN_field,NOMOR_DO_field,TANGGAL_DO_field,KODE_BARANG_field,NAMA_BARANG_field,BATCH_NO_field,QUANTITY_field,SATUAN_field,HARGA_SATUAN_field,HARGA_TOTAL_field,ID_field],
			
	        buttons: [{
                iconCls: 'icon-save',
                itemId: 'save',
                text: 'Save',
                disabled: true,
                action: 'save'
            }, {
                iconCls: 'icon-add',
				itemId: 'create',
                text: 'Create',
                action: 'create'
            }, {
                iconCls: 'icon-reset',
                text: 'Cancel',
                action: 'cancel'
            }]
        });
        
        this.callParent();
    }
});